# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import pymongo
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from mongo_config import *

mongo_client = pymongo.MongoClient(MONGO_URL, 27017)
db = mongo_client[MONGO_DB_RES]

driver = webdriver.Chrome()

def parse_page(url, number):
    contents = []
    driver.get(url)
    for i in range(0, number):
        divs = driver.find_elements_by_xpath("//div[@id='searchResult']/div[@id='ItemWrapper']/div[@class='item']")
        for div in divs:
            comurl = div.find_element_by_xpath(".//a").get_attribute('href')
            title = div.find_element_by_xpath(".//span[@class='title']").text
            price = div.find_element_by_xpath(".//span[@class='pricedetail']/strong").text
            number = div.find_element_by_xpath(".//span[@class='payNum']").text
            info = {
                'url': comurl,
                'title': title,
                'price': price,
                'number': number
            }
            print(info)
            contents.append(info)
        print('第%d页数据获取完成' % (i + 1))
        next_page = driver.find_element_by_xpath("//a[@class='page-next iconfont']").click()
        time.sleep(1)
    driver.quit()
    return contents

def save_infos(coll_name, contents):
    collection_res = db[coll_name]
    try:
        collection_res.insert_many(contents, ordered=False)
    except Exception as e:
        print(e)


def main():
    # url = "https://uland.taobao.com/sem/tbsearch?refpid=mm_26632258_3504122_32538762&keyword=%E5%A5%B3%E8%A3%85&clk1=44c369a534bf95506aa0a87518971645&upsid=44c369a534bf95506aa0a87518971645"
    seach_name = input('搜索商品名：')
    url = "https://uland.taobao.com/sem/tbsearch?refpid=mm_26632258_3504122_32538762&keyword=" + seach_name + "&clk1=44c369a534bf95506aa0a87518971645&upsid=44c369a534bf95506aa0a87518971645"
    number = 3
    contents = parse_page(url, number)
    save_infos(seach_name, contents)

if __name__ == '__main__':
    main()